package com.athena.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.athena.common.core.domain.entity.SysDept;
import com.athena.common.core.domain.model.LoginUser;
import com.athena.common.helper.DataBaseHelper;
import com.athena.common.helper.LoginHelper;
import com.athena.common.utils.DateUtils;
import com.athena.common.utils.StreamUtils;
import com.athena.common.utils.StringUtils;
import com.athena.common.core.page.TableDataInfo;
import com.athena.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.athena.crm.constant.CommonConstant;
import com.athena.crm.domain.*;
import com.athena.crm.domain.bo.*;
import com.athena.crm.domain.vo.*;
import com.athena.crm.mapper.CrmClueFollrecordsMapper;
import com.athena.crm.mapper.CrmClueMapper;
import com.athena.crm.mapper.CrmCustomerFollrecordsMapper;
import com.athena.crm.service.ICrmClueFollrecordsService;
import com.athena.crm.service.ICrmCustomerFollrecordsService;
import com.athena.system.mapper.SysDeptMapper;
import com.athena.system.service.ISysDeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.athena.crm.mapper.CrmCustomerMapper;
import com.athena.crm.service.ICrmCustomerService;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * CRM客户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CrmCustomerServiceImpl implements ICrmCustomerService {

    private final CrmCustomerMapper baseMapper;

    private final CrmCustomerFollrecordsMapper crmCustomerFollrecordsMapper;

    private final CrmClueFollrecordsMapper crmClueFollrecordsMapper;

    private final CrmClueMapper crmClueMapper;

    private final SysDeptMapper deptMapper;

    private final ICrmCustomerFollrecordsService crmCustomerFollrecordsService;

    private final ISysDeptService sysDeptService;

    /**
     * 查询CRM客户
     */
    @Override
    public CrmCustomerVo queryById(Long id){
        CrmCustomerVo crmCustomerVo = baseMapper.selectVoById(id);
        //查询跟进记录
        CrmCustomerFollrecordsBo crmCustomerFollrecordsBo = new CrmCustomerFollrecordsBo();
        crmCustomerFollrecordsBo.setCustomerId(id + "");
        List<CrmCustomerFollrecordsVo> crmCustomerFollrecordsVos = crmCustomerFollrecordsService.queryList(crmCustomerFollrecordsBo);
        List<CrmClueFollrecordsGroupVo> crmClueFollrecordsGroupVos = new ArrayList<>();
        for (CrmCustomerFollrecordsVo crmCustomerFollrecordsVo :crmCustomerFollrecordsVos) {
            CrmClueFollrecordsGroupVo crmClueFollrecordsGroupVo = new CrmClueFollrecordsGroupVo();
            Date createTime = crmCustomerFollrecordsVo.getCreateTime();
            String createTimeStr = DateUtils.parseDateToStr("yyyy-MM-dd", createTime);
            Date key = DateUtils.dateTime("yyyy-MM-dd", createTimeStr);
            crmClueFollrecordsGroupVo.setDatekey(key);
            crmClueFollrecordsGroupVo.setCreateTime(createTime);
            crmClueFollrecordsGroupVo.setContent(crmCustomerFollrecordsVo.getFollowContent());
            crmClueFollrecordsGroupVos.add(crmClueFollrecordsGroupVo);
            log.info("==={}===={}===", crmCustomerFollrecordsVo.getCustomerId(), crmCustomerFollrecordsVo.getFollowContent());
        }
        List<CrmClueTimeLineVo> crmClueTimeLineVos1 = new ArrayList<>();
        Map<Date, List<CrmClueFollrecordsGroupVo>> groupMap = crmClueFollrecordsGroupVos.stream().filter(a -> a != null && a.getDatekey() != null).collect(Collectors.groupingBy(CrmClueFollrecordsGroupVo::getDatekey));
        Map<Date, List<CrmClueFollrecordsGroupVo>> sortedMap = groupMap.entrySet().stream()
            .sorted(Collections.reverseOrder(Map.Entry.comparingByKey()))
            .collect(
                Collectors.toMap(
                    Map.Entry::getKey,
                    Map.Entry::getValue,
                    (oldVal, newVal) -> oldVal,
                    LinkedHashMap::new
                )
            );
        sortedMap.forEach((key, value) -> {
            Date date = key;
            String yearmonth = DateUtils.getYearMonth(date);
            String dateStr = DateUtils.getDateStr(date);
            CrmClueTimeLineVo crmClueTimeLineVo1 = getCrmClueTimeLineVo(yearmonth, dateStr, value);
            crmClueTimeLineVos1.add(crmClueTimeLineVo1);
            log.info("Key: {}, Date: {}, createBy: {}, valueSize {}", key, date, key, value.size());
        });
        Map<String, List<CrmClueTimeLineMonthVo>> map = new HashMap<>();
        for (CrmClueTimeLineVo crmClueTimeLineVo:crmClueTimeLineVos1) {
            List<CrmClueTimeLineMonthVo> resultCrmClueTimeLineVos = map.get(crmClueTimeLineVo.getYearmonth());
            if (CollectionUtils.isEmpty(resultCrmClueTimeLineVos)) {
                resultCrmClueTimeLineVos = crmClueTimeLineVo.getBody();
                map.put(crmClueTimeLineVo.getYearmonth(), resultCrmClueTimeLineVos);
            } else {
                List<CrmClueTimeLineMonthVo> body = crmClueTimeLineVo.getBody();
                resultCrmClueTimeLineVos.addAll(body);
            }
            log.info("Key: {}, Date: {}, createBy: {}, valueSize {}", crmClueTimeLineVo.getYearmonth(), crmClueTimeLineVo.getBody().size());
        }
        List<CrmClueTimeLineVo> resultCrmClueTimeLineVos = new ArrayList<>();
        map.forEach((key, value) -> {
            CrmClueTimeLineVo crmClueTimeLineVo = new CrmClueTimeLineVo();
            crmClueTimeLineVo.setYearmonth(key);
            crmClueTimeLineVo.setBody(value);
            resultCrmClueTimeLineVos.add(crmClueTimeLineVo);
        });
        resultCrmClueTimeLineVos.sort(Comparator.comparing(CrmClueTimeLineVo::getYearmonth).reversed());
        crmCustomerVo.setCrmClueTimeLineVos(resultCrmClueTimeLineVos);
        return crmCustomerVo;
    }

    private CrmClueTimeLineVo getCrmClueTimeLineVo(String yearmonth, String date, List<CrmClueFollrecordsGroupVo> groupVos) {
        CrmClueTimeLineVo crmClueTimeLineVo1 = new CrmClueTimeLineVo();
        crmClueTimeLineVo1.setYearmonth(yearmonth);
        List<CrmClueTimeLineMonthVo> crmClueTimeLineMonthVos = new ArrayList<>();
        CrmClueTimeLineMonthVo crmClueTimeLineMonthVo1 = getCrmClueTimeLineMonthVo(date, groupVos);
        crmClueTimeLineMonthVos.add(crmClueTimeLineMonthVo1);
        crmClueTimeLineVo1.setBody(crmClueTimeLineMonthVos);
        return crmClueTimeLineVo1;
    }

    private CrmClueTimeLineMonthVo getCrmClueTimeLineMonthVo(String date, List<CrmClueFollrecordsGroupVo> groupVos) {
        CrmClueTimeLineMonthVo crmClueTimeLineMonthVo1 = new CrmClueTimeLineMonthVo();
        crmClueTimeLineMonthVo1.setDate(date);
        List<CrmClueTimeLineMonthChildrenVo> monthChildren = getCrmClueTimeLineMonthChildrenVos(groupVos);
        monthChildren.sort(Comparator.comparing(CrmClueTimeLineMonthChildrenVo::getTime).reversed());
        crmClueTimeLineMonthVo1.setChildren(monthChildren);
        return crmClueTimeLineMonthVo1;
    }

    private List<CrmClueTimeLineMonthChildrenVo> getCrmClueTimeLineMonthChildrenVos(List<CrmClueFollrecordsGroupVo> groupVos) {
        List<CrmClueTimeLineMonthChildrenVo> monthChildren = new ArrayList<>();
        for (CrmClueFollrecordsGroupVo crmClueFollrecordsGroupVo :groupVos) {
            CrmClueTimeLineMonthChildrenVo crmClueTimeLineMonthChildrenVo = new CrmClueTimeLineMonthChildrenVo();
            crmClueTimeLineMonthChildrenVo.setTime(DateUtils.parseDateToStr("HH:mm:ss", crmClueFollrecordsGroupVo.getCreateTime()));
            crmClueTimeLineMonthChildrenVo.setTitle(crmClueFollrecordsGroupVo.getContent());
            monthChildren.add(crmClueTimeLineMonthChildrenVo);
        }
        return monthChildren;
    }

    /**
     * 查询CRM客户列表
     */
    @Override
    public TableDataInfo<CrmCustomerVo> queryPageList(LoginUser loginUser, CrmCustomerBo bo, PageQuery pageQuery) {
        String searchField = "id, name, com_name as comName, phone, label, type, industry, approval_status as approvalStatus, approval_submit_time as approvalSubmitTime, approval_submit_time as approvalSubmitTime, contacts, address, follow_status as followStatus, customer_sources as customerSources, head, dept_id as deptId, former_head as formerHead, former_dept_id as formerDeptId, former_high_seas as formerHighSeas, current_high_seas as currentHighSeas, into_hight_seas_time as intoHightSeasTime, latest_follow_records as latestFollowRecords, reality_follow_records as realityFollowRecords, DATEDIFF(NOW(), reality_follow_records)  as notFollowDays, next_follow_time as nextFollowTime, channel, transfer_customer_time as transferCustomerTime, last_call_time as lastCallTime, create_by as createBy, create_time as createTime, update_by updateBy, update_time as updateTime";
        LambdaQueryWrapper<CrmCustomer> queryWrapper = new QueryWrapper<CrmCustomer>()
            .select(searchField).lambda();
        LambdaQueryWrapper<CrmCustomer> lqw = buildNewQueryWrapper(queryWrapper, loginUser, bo);
        lqw.orderByDesc(CrmCustomer::getUpdateTime);
        Page<CrmCustomerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<CrmCustomerVo> crmCustomerVos = result.getRecords();
        for (CrmCustomerVo crmCustomerVo:crmCustomerVos) {
            StringBuffer deptPath = new StringBuffer("");
            String deptPaht = sysDeptService.getDeptPath(crmCustomerVo.getDeptId(), "");
            crmCustomerVo.setDeptName(deptPaht);
        }
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<CrmCustomer> buildNewQueryWrapper(LambdaQueryWrapper<CrmCustomer> lqw, LoginUser loginUser, CrmCustomerBo bo) {
        Map<String, Object> params = bo.getParams();
        lqw.eq(CrmCustomer::getCustomerStatus, "客户");
        lqw.like(StringUtils.isNotBlank(bo.getName()), CrmCustomer::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), CrmCustomer::getLabel, bo.getLabel());
        if (StringUtils.isNotBlank(bo.getType())) {
            String[] customerType = bo.getType().split(",");
            lqw.in(StringUtils.isNotBlank(bo.getType()), CrmCustomer::getType, customerType);
        }
        if (bo.getHeads() != null) {
            lqw.in(CrmCustomer::getHead, bo.getHeads());
        }
        if (bo.getDeptIds() != null) {
            lqw.in(CrmCustomer::getDeptId, bo.getDeptIds());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getApprovalStatus()), CrmCustomer::getApprovalStatus, bo.getApprovalStatus());
        lqw.eq(bo.getApprovalSubmitTime() != null, CrmCustomer::getApprovalSubmitTime, bo.getApprovalSubmitTime());
        lqw.eq(bo.getApprovalCompleteTime() != null, CrmCustomer::getApprovalCompleteTime, bo.getApprovalCompleteTime());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), CrmCustomer::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), CrmCustomer::getAddress, bo.getAddress());
        if (StringUtils.isNotBlank(bo.getFollowStatus())) {
            String[] followStatus = bo.getFollowStatus().split(",");
            lqw.in(StringUtils.isNotBlank(bo.getFollowStatus()), CrmCustomer::getFollowStatus, followStatus);
        }
        if (StringUtils.isNotBlank(bo.getCustomerSources())) {
            String[] customerSources = bo.getCustomerSources().split(",");
            lqw.in(StringUtils.isNotBlank(bo.getCustomerSources()), CrmCustomer::getCustomerSources, customerSources);
        }
        lqw.eq(StringUtils.isNotBlank(bo.getHead()), CrmCustomer::getHead, bo.getHead());
        if (bo.getDeptId() != null) {
            lqw.eq(CrmCustomer::getDeptId, bo.getDeptId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getFormerHead()), CrmCustomer::getFormerHead, bo.getFormerHead());
        if(bo.getFormerDeptId() != null) {
            lqw.eq(StringUtils.isNotBlank(bo.getFormerDeptId().toString()), CrmCustomer::getFormerDeptId, bo.getFormerDeptId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getFormerHighSeas()), CrmCustomer::getFormerHighSeas, bo.getFormerHighSeas());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrentHighSeas()), CrmCustomer::getCurrentHighSeas, bo.getCurrentHighSeas());
        lqw.eq(bo.getIntoHightSeasTime() != null, CrmCustomer::getIntoHightSeasTime, bo.getIntoHightSeasTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLatestFollowRecords()), CrmCustomer::getLatestFollowRecords, bo.getLatestFollowRecords());
        lqw.eq(bo.getRealityFollowRecords() != null, CrmCustomer::getRealityFollowRecords, bo.getRealityFollowRecords());
        lqw.eq(bo.getNotFollowDays() != null, CrmCustomer::getNotFollowDays, bo.getNotFollowDays());
        lqw.eq(bo.getNextFollowTime() != null, CrmCustomer::getNextFollowTime, bo.getNextFollowTime());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), CrmCustomer::getChannel, bo.getChannel());
        lqw.eq(bo.getTransferCustomerTime() != null, CrmCustomer::getTransferCustomerTime, bo.getTransferCustomerTime());
        lqw.eq(bo.getLastCallTime() != null, CrmCustomer::getLastCallTime, bo.getLastCallTime());
        lqw.and(ObjectUtil.isNotNull(loginUser.getDeptId()), w -> {
            List<SysDept> deptList = deptMapper.selectList(new LambdaQueryWrapper<SysDept>()
                .select(SysDept::getDeptId)
                .apply(DataBaseHelper.findInSet(loginUser.getDeptId(), "ancestors")));
            List<Long> ids = StreamUtils.toList(deptList, SysDept::getDeptId);
            ids.add(loginUser.getDeptId());
            w.in(CrmCustomer::getDeptId, ids);
        });
        return lqw;
    }

    /**
     * 查询CRM客户列表
     */
    @Override
    public List<CrmCustomerVo> queryList(CrmCustomerBo bo) {
        LambdaQueryWrapper<CrmCustomer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CrmCustomer> buildQueryWrapper(CrmCustomerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CrmCustomer> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), CrmCustomer::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), CrmCustomer::getLabel, bo.getLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), CrmCustomer::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovalStatus()), CrmCustomer::getApprovalStatus, bo.getApprovalStatus());
        lqw.eq(bo.getApprovalSubmitTime() != null, CrmCustomer::getApprovalSubmitTime, bo.getApprovalSubmitTime());
        lqw.eq(bo.getApprovalCompleteTime() != null, CrmCustomer::getApprovalCompleteTime, bo.getApprovalCompleteTime());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), CrmCustomer::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), CrmCustomer::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getFollowStatus()), CrmCustomer::getFollowStatus, bo.getFollowStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomerSources()), CrmCustomer::getCustomerSources, bo.getCustomerSources());
        lqw.eq(StringUtils.isNotBlank(bo.getHead()), CrmCustomer::getHead, bo.getHead());
        if (bo.getDeptId() != null) {
            lqw.eq(CrmCustomer::getDeptId, bo.getDeptId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getFormerHead()), CrmCustomer::getFormerHead, bo.getFormerHead());
        if(bo.getFormerDeptId() != null) {
            lqw.eq(StringUtils.isNotBlank(bo.getFormerDeptId().toString()), CrmCustomer::getFormerDeptId, bo.getFormerDeptId());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getFormerHighSeas()), CrmCustomer::getFormerHighSeas, bo.getFormerHighSeas());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrentHighSeas()), CrmCustomer::getCurrentHighSeas, bo.getCurrentHighSeas());
        lqw.eq(bo.getIntoHightSeasTime() != null, CrmCustomer::getIntoHightSeasTime, bo.getIntoHightSeasTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLatestFollowRecords()), CrmCustomer::getLatestFollowRecords, bo.getLatestFollowRecords());
        lqw.eq(bo.getRealityFollowRecords() != null, CrmCustomer::getRealityFollowRecords, bo.getRealityFollowRecords());
        lqw.eq(bo.getNotFollowDays() != null, CrmCustomer::getNotFollowDays, bo.getNotFollowDays());
        lqw.eq(bo.getNextFollowTime() != null, CrmCustomer::getNextFollowTime, bo.getNextFollowTime());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), CrmCustomer::getChannel, bo.getChannel());
        lqw.eq(bo.getTransferCustomerTime() != null, CrmCustomer::getTransferCustomerTime, bo.getTransferCustomerTime());
        lqw.eq(bo.getLastCallTime() != null, CrmCustomer::getLastCallTime, bo.getLastCallTime());
        return lqw;
    }

    /**
     * 新增CRM客户
     */
    @Override
    public Boolean insertByBo(CrmCustomerBo bo) {
        CrmCustomer add = BeanUtil.toBean(bo, CrmCustomer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改CRM客户
     */
    @Override
    public Boolean updateByBo(CrmCustomerBo bo) {
        CrmCustomer update = BeanUtil.toBean(bo, CrmCustomer.class);
        validEntityBeforeSave(update);
        boolean result = baseMapper.updateById(update) > 0;
        if (result) {
            CrmCustomerFollrecords crmCustomerFollrecords = new CrmCustomerFollrecords();
            crmCustomerFollrecords.setCustomerId(bo.getId() + "");
            crmCustomerFollrecords.setFlowType(CommonConstant.FOLLOW_TYPE_OPERATE_LOG);
            crmCustomerFollrecords.setFollowContent("更新客户[" + bo.getName() + "]基本信息");
            crmCustomerFollrecords.setFollowUser(LoginHelper.getLoginUser().getUsername());
            crmCustomerFollrecordsMapper.insert(crmCustomerFollrecords);
        }
        return result;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CrmCustomer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除CRM客户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean insertByBbo(CrmClueTransferBo bbo) {
        List<CrmCustomer> crmCustomers = new ArrayList<>();
        List<CrmClue> crmClues = new ArrayList<>();
        String[] idList = bbo.getIds().split(",");
        List<CrmClueVo> crmClueVos = crmClueMapper.selectVoBatchIds(Arrays.asList(idList));
        for (CrmClueVo crmClueVo:crmClueVos) {
            CrmCustomer crmCustomer = new CrmCustomer();
            crmCustomer.setName(crmClueVo.getName());
            crmCustomer.setPhone(crmClueVo.getPhone());
            log.info(crmClueVo.getName());
            crmCustomers.add(crmCustomer);
            CrmClue crmClue = BeanUtil.toBean(crmClueVo, CrmClue.class);
            crmClue.setClueStatus("已转客户");
            crmClues.add(crmClue);
        }
        baseMapper.insertBatch(crmCustomers);
        crmClueMapper.updateBatchById(crmClues);
        return true;
    }

    @Override
    public int clueTransferCustomer(CrmClueTransferCustomerBo bo) {
        CrmClueVo crmClueVo = crmClueMapper.selectVoById(bo.getId());
        if (crmClueVo != null) {
            //生成客户信息
            CrmCustomer crmCustomer = new CrmCustomer();
            crmCustomer.setName(crmClueVo.getName());
            crmCustomer.setPhone(crmClueVo.getPhone());
            crmCustomer.setIndustry(bo.getIndustry());
            crmCustomer.setComName(bo.getComName());
            crmCustomer.setCustomerSources(bo.getCustomerSources());
            crmCustomer.setFollowStatus(bo.getFollowStatus());
            crmCustomer.setType(bo.getType());
            crmCustomer.setTransferCustomerTime(new Date());
            crmCustomer.setDeptId(LoginHelper.getLoginUser().getDeptId());
            crmCustomer.setHead(LoginHelper.getLoginUser().getUsername());
            crmCustomer.setLabel(bo.getLabel());
            crmCustomer.setCustomerStatus("客户");
            baseMapper.insert(crmCustomer);
            //客户流水记录
            CrmCustomerFollrecords crmCustomerFollrecords = new CrmCustomerFollrecords();
            crmCustomerFollrecords.setCustomerId(crmCustomer.getId() + "");
            crmCustomerFollrecords.setFlowType(CommonConstant.FOLLOW_TYPE_OPERATE_LOG);
            crmCustomerFollrecords.setFollowContent("新增客户[" + crmClueVo.getName() + "]");
            crmCustomerFollrecords.setFollowUser(LoginHelper.getLoginUser().getUsername());
            crmCustomerFollrecordsMapper.insert(crmCustomerFollrecords);
            //线索流水记录
            CrmClueFollrecords crmClueFollrecords = new CrmClueFollrecords();
            crmClueFollrecords.setClueId(crmClueVo.getId() + "");
            crmClueFollrecords.setFlowType(CommonConstant.FOLLOW_TYPE_OPERATE_LOG);
            crmClueFollrecords.setFollowContent("线索[" + crmClueVo.getName() + "]转客户");
            crmClueFollrecords.setFollowUser(LoginHelper.getLoginUser().getUsername());
            crmClueFollrecordsMapper.insert(crmClueFollrecords);
            //更新线索状态(线索 已删除)
            CrmClue crmClue = BeanUtil.toBean(crmClueVo, CrmClue.class);
            crmClue.setClueStatus("已转客户");
            crmClue.setTransferCustomerTime(new Date());
            crmClue.setFormerDeptId(crmClue.getDeptId());
            crmClue.setFormerHead(crmClue.getHead());
            crmClue.setIsDeleted("1");
            crmClueMapper.updateById(crmClue);
            return 1;
        }
        return 0;
    }

    @Override
    public CrmCustomerVo queryByNameAndPhone(String name, String phone) {
        LambdaQueryWrapper<CrmCustomer> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(name), CrmCustomer::getName, name);
        lqw.eq(StringUtils.isNotBlank(phone), CrmCustomer::getPhone, phone);
        List<CrmCustomerVo> crmCustomerVos = baseMapper.selectVoList(lqw);
        if (!CollectionUtils.isEmpty(crmCustomerVos)) {
            return crmCustomerVos.get(0);
        }
        return null;
    }
}
